/*
 *@本js功能：虚机查询
 *@本js作者：杨锡君
 *@编写时间：2017年11月16日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $tableBox = $(".tableBox");
    var $assigned = $("#assigned"); //分配状态
    var $runningStatu = $("#runningStatu"); //运行状态
    var $searchBtn = $(".searchBtn");
    var $virTotal = $(".virTotal"); //虚机总数
    var $virAllocated = $(".virAllocated"); //已分配
    var $virUnabsorbed = $(".virUnabsorbed"); //未分配
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-1\">姓名</th>" +
        "<th class=\"col-lg-1\">学号/工号</th>" +
        "<th class=\"col-lg-2\">班级名称</th>" +
        "<th class=\"col-lg-2\">课程名称</th>" +
        "<th class=\"col-lg-2\">虚拟机名称</th>" +
        "<th class=\"col-lg-1\">镜像名称</th>" +
        "<th class=\"col-lg-1\">IP地址</th>" +
        "<th class=\"col-lg-1\">状态</th>" +
        // "<th class=\"col-lg-1\">操作</th>" +
        "</tr>";
    function tabelModel(opt,startRow) {
        var str1 = "";
        if(opt.status == "运行"){
            str1 = "<td class=\"col-lg-1\"></td>";
        }else{
            str1 = "<td class=\"col-lg-1\"><input data-id=" + opt.id + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>";
        }
        var str = "<tr data-num=\"0\">" +
            str1+
            "<td class=\"col-lg-1\" title="+ opt.stuorteaName +">"+ opt.stuorteaName +"</td>" +
            "<td class=\"col-lg-1\" title="+ opt.stuorteaId +">"+ opt.stuorteaId +"</td>" +
            "<td class=\"col-lg-1\" title="+ (opt.className==null?'':opt.className) +">"+ (opt.className==null?'':opt.className) +"</td>" +
            "<td class=\"col-lg-2\" title="+ (opt.courseName==null?"":opt.courseName) +">"+ (opt.courseName==null?"":opt.courseName) +"</td>" +
            "<td class=\"col-lg-2\" title="+ opt.name +">"+ opt.name +"</td>" +
            "<td class=\"col-lg-1\" title="+ opt.imageName+">"+ opt.imageName +"</td>" +
            "<td class=\"col-lg-2\" title="+ opt.ip +">"+ opt.ip +"</td>" +
            "<td class=\"col-lg-1\" title="+ opt.status +">"+ opt.status +"</td>" +
            // "<td class=\"col-lg-1\">"+
            // "<a href=\"javascript:;\" data-id='"+ opt.id +"'   class=\"detailBtn\">替换</a>" +
            // "</td>"+
            "</tr>";
        return str;
    }
    var virMachineSch = {
        some:'',
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            var json = {
                "pageNum": 1,
                "pageSize": 10
            };
            self.some = json;
            self.tabelList(json);
            var classJson = JSON.stringify({
                "pageNow": 0,
                "pageSize": 10
            });
            $.ajax({
                type: "post",
                url: "getClassList",
                async: true,
                data: {
                    "jsonStr":classJson
                },
                success: function (msg) {
                    msg = JSON.parse(msg);
                    if (msg.result == "success") {
                        if(msg.classList!=null&&msg.classList!=undefined&&msg.classList!=[]){
                            $.map(msg.classList,function (opt) {
                                $("#className").append("<option value='"+ opt.classId +"'>"+ opt.className +"</option>")
                            });
                        }

                    }
                }
            });
            $searchBtn.off("click").on("click", function() {

                var jsonStr = {
                    "pageNum": 1,
                    "pageSize": 10
                };
                if($("#className").val()!=0){
                    jsonStr.classId = $("#className").val();
                }
                if($("#stuorteaName").val()!=''){
                    jsonStr.stuorteaName = $("#stuorteaName").val();
                }
                if($("#courseName").val()!=''){
                    jsonStr.courseName = $("#courseName").val();
                }
                self.tabelList(jsonStr);
                self.some = jsonStr;
            });
            $(".delClass").off("click").on("click", function() {
                self.delListEvent();
            });
        },
        delListEvent: function() {
            var self = this;
            if ($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要删除的虚机";
                Alert1.init();
                Alert1.show();
            } else {
                confirm1.content = "删除虚拟机将影响正在进行的实验，请确认是否删除？";
                confirm1.success = function () {
                    var list = [];
                    // var lists = [];
                    for (var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                        // lists.push(Number($(".oneCheck[value=1]").eq(i).attr("data-classid")));
                        list.push(Number($(".oneCheck[value=1]").eq(i).attr("data-id")));
                    }
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "manage/container/delete",
                        async: true,
                        data:JSON.stringify(list),
                        dataType:'JSON',
                        contentType:'application/json',
                        success: function (msg) {
                            loading1.hide();
                            if(msg.code == 200) {
                                Alert1.content = "删除虚拟机成功";
                                Alert1.success = function () {
                                    self.tabelList(self.some);
                                };
                                Alert1.init();
                                Alert1.show();
                            } else {
                                Alert1.content = msg.message;
                                Alert1.init();
                                Alert1.show();
                                return false;
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();

            }
        },
        virCount:function(){
//			查询虚拟机的数量


        },
        exchange:function(json){
            var self = this;
        },
        tabelList:function(jsons) {
            var self = this;
            loading1.show();
            // json = JSON.stringify(jsons);
            $.ajax({
                type: "get",
                url: "manage/container/page",
                data: jsons,
                async: true,
                success: function(msg) {
                    loading1.hide();
                    self.virCount();
                    // msg = JSON.parse(msg);
                    if(msg.code == 200) {
                        $tableBox.empty();
                        if(msg.data.list.length != 0) {
                            //列表
                            var tabel = "";
                            var startRow = msg.data.startRow;
                            $.map(msg.data.list, function(opt) {
                                tabel += tabelModel(opt,startRow);
                                startRow++;
                            });
                            $tableBox.append(th + tabel);
                            // self.exchange(jsons);
                            self.allCheckEvent();
                        } else {
                            var trImg = "<tr><td colspan='10'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.data.pages;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNum = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "get",
                                    url: "manage/container/page",
                                    data: jsonStr,
                                    async: true,
                                    success: function(msg) {
                                        //列表
                                        loading1.hide();
                                        // msg = JSON.parse(msg);
                                        $tableBox.empty();
                                        var tabel = "";
                                        var startRow = msg.data.startRow;
                                        $.map(msg.data.list, function(opt) {
                                            tabel += tabelModel(opt,startRow);
                                            startRow++;
                                        });
                                        $tableBox.append(th + tabel);
                                        // self.exchange(jsonStr);
                                        self.allCheckEvent();
                                    }
                                });
                            }
                        });
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });

        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }
    };
    virMachineSch.init();
});